﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace HoaDon
{
	public partial class XuatKhoNL : System.Web.UI.Page
	{
		private string ngaybd = "";
		private string ngaykt = "";
		private System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("vi-VN");
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				dttuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
				dtdenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
				spInfomation.InnerText = "";
			}
		}

		protected void btOk_Click(object sender, EventArgs e)
		{
			XuatNguyenLieu();
		}

		private void XuatNguyenLieu()
		{
			try
			{
				if (!dttuNgay.Text.ToString().Equals(""))
				{
					ngaybd = DateTime.Parse(dttuNgay.Text, cultureinfo).ToString("yyyyMMdd");
				}
				if (!dtdenNgay.Text.ToString().Equals(""))
				{
					ngaykt = DateTime.Parse(dtdenNgay.Text, cultureinfo).ToString("yyyyMMdd");
				}

				DataTable dtPh81 = new DataTable();
				if (ckAll.Checked == true)
					dtPh81 = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT Stt_rec FROM ph81 WHERE Status = '1' AND FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "'");
				else
					dtPh81 = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT Stt_rec FROM ph81 WHERE Status = '1' AND Ngay_ct >= '" + ngaybd + "' AND ph81.Ngay_ct <= '" + ngaykt + "' AND FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "'");

				for (int i = 0; i < dtPh81.Rows.Count; i++)
					TaoPhieuXuatKho(dtPh81.Rows[i]["Stt_rec"].ToString());

				spInfomation.InnerText = "Xuất chế biến thành công";
			}
			catch
			{
				spInfomation.InnerText = "Xuất chế biến không thành công";
			}
		}

		private void TaoPhieuXuatKho(string stt_Rec_HDA)
		{
			string so_ct_PXA = "";
			string stt_rec_PXA = "";

			stt_rec_PXA = GetRecID("PXA");

			DataTable dtSoCT = new DataTable();
			string text2 = "EXEC fs_HG_GET_SCT 'PXA','" + Session["FK_DatabaseID"].ToString() + "'";
			dtSoCT = SqlEXEC.GetData(Session["strConn"].ToString(), text2);
			so_ct_PXA = dtSoCT.Rows[0]["So_ct"].ToString();

			SqlEXEC.ExecCommand(Session["strConn"].ToString(), "UPDATE dmct SET No = No + 1 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND CtID = 'PXA'");
			SqlEXEC.ExecCommand(Session["strConn"].ToString(), "exec fs_InsertPXA_FromHDA '" + stt_Rec_HDA + "','" + stt_rec_PXA + "','" + so_ct_PXA + "','" + DateTime.Now.ToString("yyyyMMdd") + "','" + Session["FK_DatabaseID"].ToString() + "'");
		}

		private string GetRecID(string cVoucherCode)
		{
			try
			{
				DataTable dtstt = new DataTable();
				dtstt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT RecNumber FROM dmstt WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "'");
				double nOrder = Convert.ToDouble(dtstt.Rows[0]["RecNumber"]) + 1;
				string RecID = Convert.ToString(nOrder);
				int i = 9;
				int j = RecID.Trim().Length;
				while ((j < i))
				{
					RecID = "0" + RecID;
					j = j + 1;
				}
				DataTable dtopp = new DataTable();
				dtopp = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT val FROM options WHERE name = 'm_WrkStation'");
				RecID = dtopp.Rows[0]["val"].ToString() + RecID + cVoucherCode.Trim().ToUpper();
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), "UPDATE dmstt SET RecNumber = " + Convert.ToString(nOrder + 1) + " WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "'");
				return RecID;
			}
			catch
			{
				return "";
			}
		}
	}
}
